package com.peng.redis;

import com.google.common.base.Joiner;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * JedisTestOne
 *
 * @author lupeng10
 * @create 2024-03-18 14:46
 */
public class JedisTestOne {

    private static final Logger logger = LoggerFactory.getLogger(JedisWithHashSample.class);

    private JedisPool pool;

    public static final String KEY = "hashkey";

    @BeforeEach
    public void init() {
        pool = new JedisPool("192.168.64.1", 6379);
        // test-zhaopin01.rdb.58dns.org:50042
        // public JedisPool(GenericObjectPoolConfig<Jedis> poolConfig, String host, int port, int timeout, String password) {

        // zpad redis
        // pool = new JedisPool(new GenericObjectPoolConfig(), "test-zhaopin01.rdb.58dns.org", 50042, 10000, "208153357028f3f7");

        // decision redis
        // pool = new JedisPool(new GenericObjectPoolConfig(), "test-zhaopin01.rdb.58dns.org", 50090, 10000, "06643b881e175041");
        logger.info("JedisPool create success");
    }


    @AfterEach
    public void destroy() {
        if (pool != null) {
            pool.close();
        }
    }

    /**
     * 使用k-v形式 1W uid 占用 1215K
     * 60000000 / 10000 * 1215 = 6G
     */
    @Test
    public void t1() {
        try (Jedis jedis = pool.getResource()) {
            List<String> uidStr = FileUtils.readLines(new File("/Users/hou/Downloads/379548646_5C1798.txt"), StandardCharsets.UTF_8);
            for (String uid : uidStr) {
                String key = "crzddk-" + uid;
                jedis.setex(key, 86400, "daizhao");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void t2() {
        try (Jedis jedis = pool.getResource()) {
            List<String> uidStr = FileUtils.readLines(new File("/Users/hou/Downloads/379548646_5C1798.txt"), StandardCharsets.UTF_8);
            for (String uid : uidStr) {
                String key = "crzddk-" + uid;
                jedis.del(key);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用Hash
     * 1.02M - 833K
     */
    @Test
    public void t3() {
        try (Jedis jedis = pool.getResource()) {
            List<String> uidStr = FileUtils.readLines(new File("/Users/hou/Downloads/379548646_5C1798.txt"), StandardCharsets.UTF_8);
            for (String uids : uidStr) {
                long uid = Long.parseLong(uids);
                String key = "crzddk-" + (uid % 100);
                // jedis.hset(key, uids, "daizhao");
                jedis.del(key);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用set
     */
    @Test
    public void t4() {
        try (Jedis jedis = pool.getResource()) {
            List<String> uidStr = FileUtils.readLines(new File("/Users/hou/Downloads/379548646_5C1798.txt"), StandardCharsets.UTF_8);
            for (String uids : uidStr) {
                long uid = Long.parseLong(uids);
                String key = Joiner.on("-").join("crzddk", new SimpleDateFormat("yyyyMMdd").format(new Date()), uid % 100);
                jedis.sadd(key, uids);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
